<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="hidden" id="userId" th:value="${id}" class="layui-input">
    <div class="layui-form-item">
        <div id="roleTransfer" class="layui-layer-btn-c">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-layer-btn-c">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'common'], function () {
        let form = layui.form,
            layer = layui.layer,
            transfer = layui.transfer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function () {
            let data = {id: $('#userId').val(), roleIds: getRoleIds(transfer.getData('role'))}
            $.ajax({
                url: '/admin/user/role',
                type: 'post',
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('操作成功', {time: 500}, function () {
                            // 刷新列表
                            window.back();
                        });
                    }
                },
            });
            return false;
        });

        // 获取角色id集合
        function getRoleIds(checkedData) {
            let roleIds = [];
            $.each(checkedData, function (index, val) {
                roleIds.push(val.value);
            });
            return roleIds;
        };

        // 获取资源数据
        $.ajax({
            url: '/admin/user/role?id=' + $('#userId').val(),
            type: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    let data = res.data;
                    if (data != null) {
                        //开启复选框tree
                        transfer.render({
                            elem: '#roleTransfer',
                            id: 'role',
                            title: ['未分配角色', '已分配角色'],
                            height: 500,
                            data: data.roles,
                            value: data.roleIds,
                            showSearch: true
                        });
                    }
                }
            },
        });
    });
</script>
</body>
</html>